﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace WebApplication2.student
{
    public partial class 个人信息修改 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["xh"] != null)
            {
                if (!IsPostBack)
                {
                    bind();

                }
            }
            
        }
        private void bind()
        {
            //string xh = "";
            //if(Session["xh"]==null)
            //{

            //}
            //else
            //{
            //string xh = Request.QueryString["xh"].ToString();
            string xh = Session["xh"].ToString();
            string sql = "select * from xs where xh ='" + xh + "'";
            Database db = new Database();
            DataTable dt = db.SelectSQL(sql);
            DataRow dr = dt.Rows[0];
            lbl_xm.Text = dr["xm"].ToString();
            lbl_xb.Text = dr["xb"].ToString();
            lbl_mz.Text = dr["mz"].ToString();
            lbl_csrq.Text = dr["csrq"].ToString();
            lbl_xy.Text = dr["xy"].ToString();
            lbl_zy.Text = dr["zy"].ToString();
            lbl_bj.Text = dr["bj"].ToString();
            lbl_sj.Text = dr["sj"].ToString();
            lbl_yx.Text = dr["yx"].ToString();
            lbl_txdz.Text = dr["txdz"].ToString();
            string ph= "~\\image\\" + dr["photo"].ToString();
            
            if(ph != "~\\image\\")
            {
                Image1.ImageUrl = ph;
            }
            else
            {
                Image1.ImageUrl = "../image/男生1.jpg";
            }



        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string xm = lbl_xm.Text;
            string xb = lbl_xb.Text;
            string mz = lbl_mz.Text;

            string sql = "update xs set xb='" + xb + "',xm='" + xm + "',mz='" + mz + "' where xh='001'";
            Database db = new Database();
            db.ExecSQL(sql);
            Response.Redirect("个人信息.aspx");

            
        }
        private void updatepath(string xh, string filepath)
        {
            string sql = "update xs set photo='" + filepath + "' where xh='" + xh + "'";

            Database db = new Database();
            db.ExecSQL(sql);


           
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            if (Session["xh"] != null)
            {
                if (FileUpload1.HasFile)
                {
                    int idex = FileUpload1.FileName.LastIndexOf(".");//返回指定字符在此字符串中最后一次出现处的索引
                    string filelastname = FileUpload1.FileName.Substring(idex + 1);//方法返回字符串的子字符串
                    if (filelastname.ToLower() == "jpg")// 把给定的字母转换为小写字母。
                    {
                        string path = Server.MapPath("../image");
                        string file = path + "\\" + FileUpload1.FileName;
                        FileUpload1.SaveAs(file);

                        //保存到数据库里面
                        updatepath(Session["xh"].ToString(), FileUpload1.FileName);


                        Image1.ImageUrl = "~\\image\\" + FileUpload1.FileName;
                        Label1.Text = "上传成功";

                    }
                    else
                    {
                        Label1.Text = "请选择jpg格式的文件";
                    }
                }
                else
                {
                    Label1.Text = "请选择文件后再上传";
                }
            }
            else
            {
                Label1.Text = "你还没有登入成功";
            }
        }


    }
}